<template>
  <div class="g-page-container">
    <PageHeader title="商机转派审批" />
    <div class="g-content-container">
      <SearchForm ref="search-form" @search="getList(1)" />
      <ButtonGroup :options="buttons" @click="handleButtonClick" />
      <TableList
        :scroll="{ x: '100%', y: 'calc(100vh - 400px)' }"
        :pagination="paginationMixin"
        :loading="loading"
        :data-source="dataSource"
        :row-selection="rowSelectionMixin"
        @change="handleTableChange"
        @number-click="handleNumberClick"
      />
    </div>

    <component
      :is="currentComponent"
      :visible.sync="visibleDrawer"
      :current-node="currentNode"
      :detail="detail"
      :plan-id="planId"
      :click-info="clickInfo"
      @fresh="getList"
      @value="getEdit"
    />
    <DialogTransferClue
      :visible.sync="visibleModal"
      :plan-id="planId"
      number="商机编号"
      type="businessTransfer"
      :selecteds="selectedInfos"
      @fresh="getList"
    />
  </div>
</template>

<script>
import {
  postBusinessTransferList,
  postBusinessTransferDetail,
} from '@/api/business';
import ButtonGroup from '@/components/button/ButtonGroup.vue';
import mixinTable from '@/mixins/mixin-table.js';
import SearchForm from './components/SearchForm.vue';
import TableList from './components/TableList.vue';
import DialogTransferClue from '@/components/dialog/DialogTransferClue.vue';
import DrawerSaleTransfer from './components/DrawerSaleTransfer.vue';
import DrawerEnd from './components/DrawerEnd.vue';

export default {
  components: {
    ButtonGroup,
    SearchForm,
    TableList,
    DialogTransferClue,
    DrawerSaleTransfer,
    DrawerEnd,
  },
  mixins: [mixinTable],
  data() {
    return {
      clickInfo: {},
      selectedInfos: [],
      columnKey: '',
      visibleModal: false,
      visibleDrawer: false,
      currentComponent: 'DrawerEnd',
      detail: {},
      planId: undefined,
      asc: true,
    };
  },
  computed: {
    buttons() {
      const disabled = !this.rowSelectionMixin.selectedRowKeys.length;
      return [
        {
          key: 'retweet',
          label: '转他人处理',
          icon: 'retweet',
          authKey: 'business-transfer-button-retweet',
          disabled,
        },
      ];
    },
  },
  watch: {
    visibleModal: {
      handler(val) {
        if (!val) {
          this.rowSelectionMixin.selectedRowKeys = [];
        }
      },
    },
  },
  created() {
    this.getList();
  },

  methods: {
    // async getPlanDetail(planId) {
    //   const result = await getPlanDetail({ planId });
    //   this.detail = result?.data.data;
    // },
    getSearchForm() {
      const form = this.$refs['search-form']?.form || {};
      const page = {
        current: this.paginationMixin.current,
        pageSize: this.paginationMixin.pageSize,
        asc: this.asc,
        column: this.columnKey,
      };
      return { ...form, ...page, bizModule: 'biz', bizType: '5' };
    },
    async getList(val) {
      if (val) {
        this.paginationMixin.current = 1;
      }
      const form = this.getSearchForm();
      this.loading = true;
      const result = await postBusinessTransferList(form).finally(
        () => (this.loading = false)
      );
      this.paginationMixin.total = result.data.data?.total;
      this.dataSource = result.data.data?.records;
    },
    handleButtonClick(key) {
      const fn = `${key}Callback`;
      this[fn] && this[fn]();
    },
    // 转他人处理
    async retweetCallback() {
      const selectedInfos = this.rowSelectionMixin.selectedRows
        .filter(item => item.currStatus === '2')
        .map((item, i) => {
          return {
            serialNumber: item.opportunityNo,
            currentUserId: item.handleUserId,
            planId: item.id,
          };
        });
      if (!selectedInfos.length) {
        this.$message.info(
          `仅可转审批中的数据，现有 ${selectedInfos.length} 条！`
        );
        return;
      }
      try {
        await this.$confirm(
          `仅可转审批中的数据，共 ${selectedInfos.length} 条，是否确认？`
        );
        this.visibleModal = true;
        this.selectedInfos = selectedInfos;
      } catch (error) {
        // console.log('error: ', error);
      }
    },
    async handleNumberClick(record) {
      let type = record.path ? 'stage' : 'nodeCode';
      if (record[type] === '1' && record.isCurrentProcessor) {
        this.currentComponent = 'DrawerSaleTransfer';
      } else {
        this.currentComponent = 'DrawerEnd';
      }
      const res = await postBusinessTransferDetail(record.transferNo);
      this.detail = res.data.data;
      this.planId = this.detail.id;
      this.visibleDrawer = true;
    },
    handleTableChange(page, filters, sorter, { currentDataSource }) {
      this.asc = sorter.order === 'ascend' ? true : false;
      this.columnKey = sorter.columnKey;
      this.paginationMixin = page;
      this.getList();
    },
  },
};
</script>
